package com.markus.code.exam.didi;

import java.util.Scanner;

/**
 * Author:markusZhang
 * Date:Create in 2020/9/13 20:45
 * todo:
 */
public class DalaoMain {
    /**
     * 找出b的父亲
     * @param b
     * @param f
     * @return
     */
    static int get(int b, int f[]) {
        if (f[b] == b) {
            return b;
        } else {
            f[b] = get(f[b], f);
            return f[b];
        }
    }

    /**
     * 找出a的祖宗
     * @param a
     * @param f
     * @return
     */
    static int find(int a, int[] f) {
        if (f[a] == a) {
            return a;
        } else {
            return find(f[a], f);
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        for (int s = 1; s <= t; s++) {
            //f代表父亲
            int f[] = new int[10010];
            //p(i) 代表有多少个节点指向i
            int p[] = new int[10010];
            int n, m, k, a, b, mk;
            n = scanner.nextInt();
            m = scanner.nextInt();
            k = scanner.nextInt();
            for (int i = 1; i <= n; i++) {
                f[i] = i;
            }
            for (int i = 1; i <= m; i++) {
                a = scanner.nextInt();
                b = scanner.nextInt();
                mk = scanner.nextInt();
                if (mk <= k) {
                    //找出b的祖宗k1
                    int k1 = find(b, f);
                    //把k1指向a的父亲
                    f[k1] = get(a, f);
                }
            }
            for (int i = 1; i <= n; i++) {
                f[i] = find(f[i], f);
            }
            for (int i = 1; i <= n; i++) {
                p[f[i]]++;
            }
            int ans = 0;
            for (int i = 0; i <= 10000; i++) {
                if (p[i] != 0) {
                    ans++;
                }
            }
            if (ans == 1) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}
